package com.ruoyi.web.controller.agoniapp;


import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.JwtUtils;
import com.ruoyi.common.utils.TokenUser;
import com.ruoyi.system.pojo.dto.AddRewardPunishmentDTO;
import com.ruoyi.system.pojo.entity.Reward;
import com.ruoyi.system.service.IAgoniRewardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/agoniapp/rewardPunishment")
public class RewardController {
    @Autowired
    private IAgoniRewardService rewardService;

    //TODO:后台管理的奖惩管理页面
    @RequestMapping("/listRewardPunishments")
    public AjaxResult rewardList() {
        return AjaxResult.success(rewardService.selectRewardList());
        }


    //TODO:用户的奖惩查询页面
    @GetMapping("/getRewardPunishmentByUserId")
    public AjaxResult getRewardPunishmentByUserId(HttpServletRequest request) {
        String token = request.getHeader("Authorization");

        if (token == null || !token.startsWith("Bearer ")) {
            log.warn("Token is missing or invalid");
            return AjaxResult.error("Token is missing or invalid");
        }

        token = token.substring(7); // 去掉 "Bearer " 前缀

        log.info("Token: " + token);
        TokenUser tokenUser = JwtUtils.getTokenUser(token);

        if (tokenUser == null) {
            log.warn("Invalid token");
            return AjaxResult.error("Invalid token");
        }

        Long userId = Long.valueOf(tokenUser.getId());
        log.info("用户 ID: " + userId);

        // 这里可以添加获取奖惩记录的逻辑
      List<Reward> rewardList = rewardService.selectRewardListByUserId(userId);


        return AjaxResult.success(rewardList);
    }


    //TODO:App端奖惩添加接口
    @PostMapping("/addRewardPunishment")
    public AjaxResult addRewardPunishment(@RequestBody AddRewardPunishmentDTO addRewardPunishmentDTO, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (token == null || !token.startsWith("Bearer ")) {
            log.warn("Token is missing or invalid");
            return AjaxResult.error("Token is missing or invalid");
        }

        token = token.substring(7); // 去掉 "Bearer " 前缀

        log.info("Token: " + token);
        TokenUser tokenUser = JwtUtils.getTokenUser(token);

        if (tokenUser == null) {
            log.warn("Invalid token");
            return AjaxResult.error("Invalid token");
        }

        Long userId = Long.valueOf(tokenUser.getId());
        log.info("用户 ID: " + userId);

       int result = rewardService.insertReward(addRewardPunishmentDTO, userId);
        return AjaxResult.success();
    }
    //TODO:App端奖惩删除接口
    @DeleteMapping("/deleteRewardPunishment/{id}")
    public AjaxResult deleteRewardPunishment(@PathVariable("id") Long id) {
        int result = rewardService.deleteRewardById(id);
       return AjaxResult.success();
        }

}
